package com.example.management.system.service.impl


import com.example.management.common.domain.Tree
import com.example.management.common.utils.BuildTree
import com.example.management.system.dao.DeptDao
import com.example.management.system.domain.DeptDO
import com.example.management.system.service.DeptService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

import java.util.ArrayList
import java.util.HashMap


@Service
class DeptServiceImpl : DeptService {
    @Autowired
    private val sysDeptMapper: DeptDao? = null

    // 默认顶级菜单为０，根据数据库实际情况调整
    override val tree: Tree<DeptDO>
        get() {
            val trees = ArrayList<Tree<DeptDO>>()
            val sysDepts = sysDeptMapper!!.list(HashMap<String, Any>(16))
            for (sysDept in sysDepts) {
                val tree = Tree<DeptDO>()
                tree.id=(sysDept.deptId.toString())
                tree.parentId = (sysDept.parentId.toString())
                tree.text = (sysDept.name)
                val state = HashMap<String, Any>(16)
                state["opened"] = true
                tree.state=(state)
                trees.add(tree)
            }
            return BuildTree.build(trees)!!
        }

    override operator fun get(deptId: Long?): DeptDO {
        return sysDeptMapper!!.get(deptId)
    }

    override fun list(map: Map<String, Any>): List<DeptDO> {
        return sysDeptMapper!!.list(map)
    }

    override fun count(map: Map<String, Any>): Int {
        return sysDeptMapper!!.count(map)
    }

    override fun save(sysDept: DeptDO): Int {
        return sysDeptMapper!!.save(sysDept)
    }

    override fun update(sysDept: DeptDO): Int {
        return sysDeptMapper!!.update(sysDept)
    }

    override fun remove(deptId: Long?): Int {
        return sysDeptMapper!!.remove(deptId)
    }

    override fun batchRemove(deptIds: Array<Long>): Int {
        return sysDeptMapper!!.batchRemove(deptIds)
    }

    override fun checkDeptHasUser(deptId: Long?): Boolean {
        // TODO Auto-generated method stub
        //查询部门以及此部门的下级部门
        val result = sysDeptMapper!!.getDeptUserNumber(deptId)
        return if (result == 0) true else false
    }

}
